unit uFrmSMART;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.ComCtrls, Vcl.Menus;

type
  TFrmSMART = class(TForm)
    lvSMART: TListView;
    pmSMART: TPopupMenu;
    miRefresh: TMenuItem;
    miEnable: TMenuItem;
    miDisable: TMenuItem;
    miEnAutoSave: TMenuItem;
    miDiAutoSave: TMenuItem;
    // procedure CreateParams(var Params: TCreateParams); override;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure miRefreshClick(Sender: TObject);
    procedure miEnableClick(Sender: TObject);
    procedure miDisableClick(Sender: TObject);
    procedure miEnAutoSaveClick(Sender: TObject);
    procedure miDiAutoSaveClick(Sender: TObject);
    procedure lvSMARTCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);
    procedure lvSMARTCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
  private
    { Private declarations }
  public
    IsThreadActive: Boolean;
  end;

var
  FrmSMART: TFrmSMART;
  TitleFrmSMART: string;

implementation

uses
  uThreadManager, uThreadSMART;

{$R *.dfm}

{
  procedure TFrmSMART.CreateParams(var Params: TCreateParams);
  begin
  inherited;
  Params.WndParent := 0;
  end;
}
procedure TFrmSMART.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TFrmSMART.FormDestroy(Sender: TObject);
begin
  FrmSMART := nil;
end;

procedure TFrmSMART.FormShow(Sender: TObject);
var
  MethodName: string;
  StartAddress: Pointer;
begin
  if ThreadManager.IsActive then
    Exit;
  MethodName := 'InitSMART';
  StartAddress := TThreadSMART.MethodAddress(MethodName);
  if StartAddress <> nil then
    ThreadManager.Run(StartAddress);
end;

procedure TFrmSMART.lvSMARTCustomDrawItem(Sender: TCustomListView; Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  ID: Byte;
  ID1: set of Byte;
  ID2: set of Byte;
begin
  ID1 := [$01, $03, $04, $0A];
  ID2 := [$05, $09, $C4, $C5, $C6, $C8];
  ID := StrToInt('$' + Item.Caption);
  if ID in ID1 then
    lvSMART.Canvas.Font.Color := clGreen
  else if ID in ID2 then
    lvSMART.Canvas.Font.Color := clPurple
  else if (ID = $BE) or (ID = $C2) then
    lvSMART.Canvas.Font.Color := clBlue
  else
    lvSMART.Canvas.Font.Color := clBlack;
end;

procedure TFrmSMART.lvSMARTCustomDrawSubItem(Sender: TCustomListView; Item: TListItem; SubItem: Integer; State: TCustomDrawState; var DefaultDraw: Boolean);
var
  ID: Byte;
  ID1: set of Byte;
  ID2: set of Byte;
begin
  ID1 := [$01, $03, $04, $0A];
  ID2 := [$05, $09, $C4, $C5, $C6, $C8];
  ID := StrToInt('$' + Item.Caption);
  case SubItem of
    1:
      if ID in ID1 then
        lvSMART.Canvas.Font.Color := clGreen
      else if ID in ID2 then
        lvSMART.Canvas.Font.Color := clPurple
      else if (ID = $BE) or (ID = $C2) then
        lvSMART.Canvas.Font.Color := clBlue;
    2:
      lvSMART.Canvas.Font.Color := clTeal;
    3:
      lvSMART.Canvas.Font.Color := clBlack;
    4:
      lvSMART.Canvas.Font.Color := clOlive;
    5:
      begin
        if Item.SubItems.Strings[4] <> '0' then
        begin
          if (ID = $BE) or (ID = $C2) then
            lvSMART.Canvas.Font.Color := clBlue
          else if ID in ID2 then
          begin
            if ID = $09 then
              lvSMART.Canvas.Font.Color := clMaroon
            else
              lvSMART.Canvas.Font.Color := clRed
          end
          else
            lvSMART.Canvas.Font.Color := clMaroon;
        end;
      end;
    7:
      lvSMART.Canvas.Font.Color := clBlack;
  end;
end;

procedure TFrmSMART.miDiAutoSaveClick(Sender: TObject);
var
  MethodName: string;
  StartAddress: Pointer;
begin
  if ThreadManager.IsActive then
    Exit;
  MethodName := 'SmartDisenalbeAutoSave';
  StartAddress := TThreadSMART.MethodAddress(MethodName);
  if StartAddress <> nil then
    ThreadManager.Run(StartAddress);
end;

procedure TFrmSMART.miDisableClick(Sender: TObject);
var
  MethodName: string;
  StartAddress: Pointer;
begin
  if ThreadManager.IsActive then
    Exit;
  MethodName := 'SmartDisenalbe';
  StartAddress := TThreadSMART.MethodAddress(MethodName);
  if StartAddress <> nil then
    ThreadManager.Run(StartAddress);
end;

procedure TFrmSMART.miEnableClick(Sender: TObject);
var
  MethodName: string;
  StartAddress: Pointer;
begin
  if ThreadManager.IsActive then
    Exit;
  MethodName := 'SmartEnalbe';
  StartAddress := TThreadSMART.MethodAddress(MethodName);
  if StartAddress <> nil then
    ThreadManager.Run(StartAddress);
end;

procedure TFrmSMART.miEnAutoSaveClick(Sender: TObject);
var
  MethodName: string;
  StartAddress: Pointer;
begin
  if ThreadManager.IsActive then
    Exit;
  MethodName := 'SmartEnalbeAutoSave';
  StartAddress := TThreadSMART.MethodAddress(MethodName);
  if StartAddress <> nil then
    ThreadManager.Run(StartAddress);
end;

procedure TFrmSMART.miRefreshClick(Sender: TObject);
var
  MethodName: string;
  StartAddress: Pointer;
begin
  if ThreadManager.IsActive then
    Exit;
  MethodName := 'InitSMART';
  StartAddress := TThreadSMART.MethodAddress(MethodName);
  if StartAddress <> nil then
    ThreadManager.Run(StartAddress);
end;

end.

